import 'package:demo/model/entity/home_entity.dart';
import 'package:demo/model/entity/home_recommend_entity.dart';
import 'package:demo/model/repository/home_repository.dart';

import 'base_view_model.dart';

class HomeViewModel extends BaseViewModel {
  /// 数据集合，如轮播 分类
  HomeEntity _homeEntity = HomeEntity();

  HomeEntity get homeEntity => _homeEntity;

  set homeEntity(HomeEntity entity) {
    _homeEntity = entity;
    notifyListeners();
  }

  /// 推荐的数据
  List<LinkData> _recommendEntity = [];

  List<LinkData> get recommendEntity => _recommendEntity;

  set recommendEntity(List<LinkData> value) {
    _recommendEntity = value;
    notifyListeners();
  }

  Future<void> initData() async {
    await Future.wait([
      initBaseLayout(),
      queryRecommendLayout(30, 1),
    ]);
  }

  /// 初始化基础如banner数据 导航
  Future<void> initBaseLayout() async {
    final HomeEntity? resHomeEntity = await HomeRepository.getColumnLayout;
    if (resHomeEntity != null) homeEntity = resHomeEntity;
  }

  /// 获取对应的首页推荐数据
  Future<bool> queryRecommendLayout(int pageSize, int pageNum) async {
    final List<LinkData>? entity =
        await HomeRepository.getRecommendLayout(pageSize, pageNum);
    if (entity != null) {
      if (pageNum == 1) {
        recommendEntity = entity;
        return true;
      }
      recommendEntity = List.of(_recommendEntity)..addAll(entity);
      return true;
    }
    return false;
  }
}
